package cn.zf233.xcloud.service.receive.impl;

import cn.zf233.xcloud.config.common.ElasticSearchConfig;
import cn.zf233.xcloud.config.common.RabbitMQConfig;
import cn.zf233.xcloud.entity.File;
import cn.zf233.xcloud.service.receive.IRabbitMQMessageSend;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by zf233 on 2021/6/4
 * RabbitMQ 消息发送服务
 */
@Component
@ConditionalOnBean({RabbitMQConfig.class, ElasticSearchConfig.class})
public class RabbitMQMessageSendImpl implements IRabbitMQMessageSend {

    private final Logger logger = LoggerFactory.getLogger(RabbitMQMessageSendImpl.class);

    @Resource
    private AmqpTemplate template;

    @Override
    public void sendMessageOfESInsertOrUpdateToRabbitMQ(File file) {
        try {
            template.convertAndSend(RabbitMQConfig.XCLOUD_TOPIC_EXCHANGE_OF_ES, "ES.insert", file);
        } catch (AmqpException e) {
            e.printStackTrace();
            this.logger.error(" **** RABBIT MQ **** EXCEPTION PRODUCE IN OR UP  --------> " + e.getMessage());
        }
    }

    @Override
    public void sendMessageOfEsRemoveToRabbitMQ(File file) {
        try {
            template.convertAndSend(RabbitMQConfig.XCLOUD_TOPIC_EXCHANGE_OF_ES, "ES.remove", file);
        } catch (AmqpException e) {
            e.printStackTrace();
            this.logger.error(" **** RABBIT MQ **** EXCEPTION PRODUCE REMOVE  ----------> " + e.getMessage());

        }
    }
}